package com.rzt.config;

import com.rzt.db.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author ：cola
 * @date ：Created in 21-1-28 上午9:19
 */
@Slf4j
@Configuration
@EnableJpaRepositories(basePackages = "com.rzt.moredb.repository",
        entityManagerFactoryRef = "dynamicEntityManageFactory",
        transactionManagerRef = "dynamicTransactionManager")
public class DynamicJpaConfig {
    @Autowired
    private MasterJpaConfig masterJpaConfig;

    // ========== 动态数据源开始 ======
    @Bean(name = "dynamicDataSource")
    @Qualifier("dynamicDataSource")
    public DataSource dynamicDataSource() throws SQLException {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setDefaultTargetDataSource(masterJpaConfig.dataSource());
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
        targetDataSources.put("masterDataSource", masterJpaConfig.dataSource());
        dynamicDataSource.setTargetDataSources(targetDataSources);
        return dynamicDataSource;
    }

    @Bean(name = "dynamicEntityManage")
    @Qualifier("dynamicEntityManage")
    public EntityManager dynamicEntityManage(EntityManagerFactoryBuilder builder) throws SQLException {
        return dynamicEntityManageFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "dynamicEntityManageFactory")
    @Qualifier("dynamicEntityManageFactory")
    public LocalContainerEntityManagerFactoryBean dynamicEntityManageFactory(EntityManagerFactoryBuilder builder) throws SQLException {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(dynamicDataSource())
                .packages("com.rzt.moredb.entity").build();
        Properties jpaProperties = new Properties();
        //jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        jpaProperties.put("hibernate.physical_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        jpaProperties.put("hibernate.connection.charSet", "utf-8");
        jpaProperties.put("hibernate.show_sql", "false");
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }

    @Bean
    @Qualifier("dynamicTransactionManager")
    public PlatformTransactionManager dynamicTransactionManager(EntityManagerFactoryBuilder builder) throws SQLException {
        return new JpaTransactionManager(dynamicEntityManageFactory(builder).getObject());
    }

}
